<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片水印去除工具</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 20px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        
        .container {
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            max-width: 800px;
            width: 100%;
        }
        
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
            font-size: 2.2em;
        }
        
        .upload-area {
            border: 3px dashed #667eea;
            border-radius: 15px;
            padding: 40px;
            text-align: center;
            margin-bottom: 20px;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        
        .upload-area:hover {
            border-color: #764ba2;
            background: #f8f9ff;
        }
        
        .upload-area.dragover {
            border-color: #764ba2;
            background: #f0f8ff;
        }
        
        #fileInput {
            display: none;
        }
        
        .upload-text {
            color: #666;
            font-size: 1.1em;
            margin-bottom: 10px;
        }
        
        .canvas-container {
            margin-top: 20px;
            text-align: center;
        }
        
        canvas {
            border: 2px solid #ddd;
            border-radius: 10px;
            max-width: 100%;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        
        .controls {
            margin-top: 20px;
            text-align: center;
        }
        
        button {
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
            border: none;
            padding: 12px 30px;
            border-radius: 25px;
            font-size: 1em;
            cursor: pointer;
            margin: 0 10px;
            transition: all 0.3s ease;
        }
        
        button:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }
        
        button:disabled {
            background: #ccc;
            cursor: not-allowed;
            transform: none;
        }
        
        .brush-controls {
            margin: 20px 0;
            text-align: center;
        }
        
        .brush-controls label {
            display: inline-block;
            margin: 0 15px;
            color: #333;
            font-weight: bold;
        }
        
        input[type="range"] {
            width: 100px;
            margin: 0 10px;
        }
        
        .instructions {
            background: #f8f9ff;
            padding: 20px;
            border-radius: 10px;
            margin-bottom: 20px;
            color: #555;
        }
        
        .instructions h3 {
            color: #667eea;
            margin-top: 0;
        }
        
        .instructions ul {
            padding-left: 20px;
        }
        
        .instructions li {
            margin-bottom: 8px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🎨 图片水印去除工具</h1>
        
        <div class="instructions">
            <h3>使用说明：</h3>
            <ul>
                <li>点击上传区域或拖拽图片文件到页面</li>
                <li>图片加载后，用鼠标在水印区域涂抹</li>
                <li>调整画笔大小来精确处理</li>
                <li>处理完成后点击下载按钮保存</li>
            </ul>
        </div>
        
        <div class="upload-area" id="uploadArea">
            <div class="upload-text">
                📁 点击此处上传图片，或直接拖拽图片到此区域
            </div>
            <div style="color: #999; font-size: 0.9em;">
                支持 JPG、PNG、GIF 格式
            </div>
            <input type="file" id="fileInput" accept="image/*">
        </div>
        
        <div class="brush-controls" id="brushControls" style="display: none;">
            <label>
                画笔大小:
                <input type="range" id="brushSize" min="5" max="50" value="20">
                <span id="brushSizeValue">20</span>px
            </label>
            <label>
                透明度:
                <input type="range" id="opacity" min="0.1" max="1" step="0.1" value="1">
                <span id="opacityValue">1</span>
            </label>
        </div>
        
        <div class="canvas-container">
            <canvas id="canvas" style="display: none;"></canvas>
        </div>
        
        <div class="controls">
            <button id="resetBtn" onclick="resetCanvas()" disabled>🔄 重置</button>
            <button id="downloadBtn" onclick="downloadImage()" disabled>📥 下载处理后的图片</button>
        </div>
    </div>

    <script>
        const canvas = document.getElementById('canvas');
        const ctx = canvas.getContext('2d');
        const fileInput = document.getElementById('fileInput');
        const uploadArea = document.getElementById('uploadArea');
        const brushControls = document.getElementById('brushControls');
        const brushSize = document.getElementById('brushSize');
        const opacity = document.getElementById('opacity');
        const brushSizeValue = document.getElementById('brushSizeValue');
        const opacityValue = document.getElementById('opacityValue');
        const resetBtn = document.getElementById('resetBtn');
        const downloadBtn = document.getElementById('downloadBtn');
        
        let originalImageData = null;
        let isDrawing = false;
        let currentBrushSize = 20;
        let currentOpacity = 1;
        
        // 更新画笔大小显示
        brushSize.addEventListener('input', function() {
            currentBrushSize = this.value;
            brushSizeValue.textContent = this.value;
        });
        
        // 更新透明度显示
        opacity.addEventListener('input', function() {
            currentOpacity = this.value;
            opacityValue.textContent = this.value;
        });
        
        // 上传区域点击事件
        uploadArea.addEventListener('click', () => {
            fileInput.click();
        });
        
        // 拖拽事件
        uploadArea.addEventListener('dragover', (e) => {
            e.preventDefault();
            uploadArea.classList.add('dragover');
        });
        
        uploadArea.addEventListener('dragleave', () => {
            uploadArea.classList.remove('dragover');
        });
        
        uploadArea.addEventListener('drop', (e) => {
            e.preventDefault();
            uploadArea.classList.remove('dragover');
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                handleFile(files[0]);
            }
        });
        
        // 文件输入事件
        fileInput.addEventListener('change', (e) => {
            if (e.target.files.length > 0) {
                handleFile(e.target.files[0]);
            }
        });
        
        // 处理文件
        function handleFile(file) {
            if (!file.type.startsWith('image/')) {
                alert('请选择图片文件！');
                return;
            }
            
            const reader = new FileReader();
            reader.onload = function(e) {
                const img = new Image();
                img.onload = function() {
                    setupCanvas(img);
                };
                img.src = e.target.result;
            };
            reader.readAsDataURL(file);
        }
        
        // 设置画布
        function setupCanvas(img) {
            // 计算合适的画布大小
            const maxWidth = 700;
            const maxHeight = 500;
            let { width, height } = img;
            
            if (width > maxWidth || height > maxHeight) {
                const ratio = Math.min(maxWidth / width, maxHeight / height);
                width *= ratio;
                height *= ratio;
            }
            
            canvas.width = width;
            canvas.height = height;
            
            // 绘制原始图片
            ctx.drawImage(img, 0, 0, width, height);
            
            // 保存原始图片数据
            originalImageData = ctx.getImageData(0, 0, width, height);
            
            // 显示画布和控件
            canvas.style.display = 'block';
            brushControls.style.display = 'block';
            resetBtn.disabled = false;
            downloadBtn.disabled = false;
            
            // 添加鼠标事件
            setupCanvasEvents();
        }
        
        // 设置画布事件
        function setupCanvasEvents() {
            canvas.addEventListener('mousedown', startDrawing);
            canvas.addEventListener('mousemove', draw);
            canvas.addEventListener('mouseup', stopDrawing);
            canvas.addEventListener('mouseout', stopDrawing);
        }
        
        // 开始绘制
        function startDrawing(e) {
            isDrawing = true;
            draw(e);
        }
        
        // 绘制
        function draw(e) {
            if (!isDrawing) return;
            
            const rect = canvas.getBoundingClientRect();
            const x = e.clientX - rect.left;
            const y = e.clientY - rect.top;
            
            // 使用智能修复算法
            inpaintArea(x, y, currentBrushSize);
        }
        
        // 停止绘制
        function stopDrawing() {
            isDrawing = false;
        }
        
        // 智能修复区域
        function inpaintArea(centerX, centerY, radius) {
            const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
            const data = imageData.data;
            
            for (let y = Math.max(0, centerY - radius); y < Math.min(canvas.height, centerY + radius); y++) {
                for (let x = Math.max(0, centerX - radius); x < Math.min(canvas.width, centerX + radius); x++) {
                    const dx = x - centerX;
                    const dy = y - centerY;
                    const distance = Math.sqrt(dx * dx + dy * dy);
                    
                    if (distance <= radius) {
                        // 计算修复颜色
                        const newColor = getInpaintColor(x, y, radius);
                        const index = (y * canvas.width + x) * 4;
                        
                        // 混合原颜色和修复颜色
                        const blendFactor = (1 - distance / radius) * currentOpacity;
                        data[index] = data[index] * (1 - blendFactor) + newColor.r * blendFactor;
                        data[index + 1] = data[index + 1] * (1 - blendFactor) + newColor.g * blendFactor;
                        data[index + 2] = data[index + 2] * (1 - blendFactor) + newColor.b * blendFactor;
                    }
                }
            }
            
            ctx.putImageData(imageData, 0, 0);
        }
        
        // 获取修复颜色
        function getInpaintColor(x, y, radius) {
            const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
            const data = imageData.data;
            
            let totalR = 0, totalG = 0, totalB = 0, count = 0;
            
            // 从周围像素采样
            for (let dy = -radius * 2; dy <= radius * 2; dy += 3) {
                for (let dx = -radius * 2; dx <= radius * 2; dx += 3) {
                    const sampleX = x + dx;
                    const sampleY = y + dy;
                    
                    if (sampleX >= 0 && sampleX < canvas.width && sampleY >= 0 && sampleY < canvas.height) {
                        const distance = Math.sqrt(dx * dx + dy * dy);
                        if (distance > radius && distance < radius * 2) {
                            const index = (sampleY * canvas.width + sampleX) * 4;
                            totalR += data[index];
                            totalG += data[index + 1];
                            totalB += data[index + 2];
                            count++;
                        }
                    }
                }
            }
            
            if (count > 0) {
                return {
                    r: totalR / count,
                    g: totalG / count,
                    b: totalB / count
                };
            }
            
            // 如果没有找到合适的采样点，返回背景色
            return { r: 250, g: 240, b: 220 };
        }
        
        // 重置画布
        function resetCanvas() {
            if (originalImageData) {
                ctx.putImageData(originalImageData, 0, 0);
            }
        }
        
        // 下载图片
        function downloadImage() {
            const link = document.createElement('a');
            link.download = 'removed_watermark.png';
            link.href = canvas.toDataURL();
            link.click();
        }
    </script>
</body>
</html>